package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2023-04-21
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

    //分页查询所有敏感词
    @Override
    public ResponseResult listByName(SensitiveDto dto) {
        //使用MP查询
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper();
         //对name做非空判断
        if (StringUtils.isNotBlank(dto.getName())) {
           //根据name模糊查询
            queryWrapper.like(WmSensitive::getId, dto.getName());
        }
           //分页
        IPage<WmSensitive> iPage = new Page<>(dto.getPage(), dto.getSize());
           //实现分页查询
        IPage<WmSensitive> page = this.page(iPage, queryWrapper);
           //Long currentPage 当前页码,
           // Integer size 每页显示条数,
           // Long total 总条数,
            // T data当前页数据
        return new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());

    }


    //新增操作
    @Override
    public ResponseResult saveSensitiveWord(WmSensitive wmSensitive) {
        //先判断是否为空
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //需要判断这个频道是否存在：先查询、判断是否存在
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getId, wmSensitive.getId()); // 构建条件
        WmSensitive one = this.getOne(queryWrapper);
        if (one != null) {
        //已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //实现新增的方法
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);

        return ResponseResult.okResult();
    }

    //删除操作
    @Override
    public ResponseResult deleteSensitiveWord(Integer id) {
        //判断要删除的敏感词是否存在
        WmSensitive sensitive = this.getById(id);
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 执行删除操作
        this.removeById(id);

        return ResponseResult.okResult();

    }
}
